package com.gz.steak.modules.system.controller;

import com.gz.steak.common.aspect.anotation.Auth;
import com.gz.steak.common.web.base.BaseController;
import com.gz.steak.common.consts.Role;
import com.gz.steak.common.exception.BusinessException;
import com.gz.steak.modules.system.pojo.dataobject.SysMenu;
import com.gz.steak.modules.system.pojo.model.SysMenuRoleModel;
import com.gz.steak.utils.convert.ConvertUtil;
import com.gz.steak.utils.jwt.TokenUtil;
import com.gz.steak.common.web.result.Result;
import com.gz.steak.modules.system.service.SysMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author 刘牌
 * @Date 2020/3/6 0006 22:27
 * @Version 1.0
 */
@RequestMapping("/menu")
@RestController
public class SysMenuController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(SysMenuController.class);

    @Autowired
    private SysMenuService sysMenuService;
    /**
     * 添加菜单
     * @param
     * @return
     */
    @Auth(Role.ADMIN)
    @PostMapping("/save")
    public Result save(@RequestBody SysMenu sysMenu , HttpServletRequest request){
        String user = TokenUtil.getUser(request);
        sysMenu.setCreateBy(user)
                .setCreateTime(ConvertUtil.FormatDate(new Date()));
        sysMenuService.save(sysMenu);
        return Result.success(null);
    }

    /**
     * 获取菜单列表
     * @return
     * @throws BusinessException
     */
    @GetMapping("/get")
    public Result get(HttpServletRequest request) throws BusinessException {
        String user = TokenUtil.getUser(request);
        List<SysMenu> sysMenu = sysMenuService.findSideTree(user,0);
        return Result.success(ConvertUtil.ListToJson(sysMenu));
    }

    /**
     * 根据角色id获取角色拥有那些菜单（数组id）
     * @param id
     * @return
     */
    @GetMapping("/findRoleHasMenuById")
    public Result findRoleHasMenuById(@RequestParam int id){
        List<Integer> list = sysMenuService.findRoleHasMenuById(id);
        return Result.success(list);
    }

    /**
     * 为角色添加菜单权限(权限配置)
     * @return
     */
    @Auth(Role.EDITOR)
    @PostMapping("/saveRoleHasMenu")
    public Result saveRoleHasMenu(@RequestBody SysMenuRoleModel sysMenuRoleModel){
        sysMenuService.saveRoleHasMenu(sysMenuRoleModel);
        return Result.success("success");
    }

}
